package com.fwlst.module_home.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.fwlst.lib_base.retrofit.GoodsResponse
import com.fwlst.module_home.model.MemberViewModel
import java.math.BigDecimal
import java.math.RoundingMode

@Composable
fun GoodItem(viewModel: MemberViewModel, goods: GoodsResponse.GoodsItem) {
    val isSelected = viewModel.selectedGoods == goods
    Card(
        modifier = Modifier
            .size(120.dp, 147.dp)
            .padding(end = 20.dp)
            .clickable { viewModel.selectedGoods = goods }
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(
                    color = if (isSelected) Color(0xFF9EC4FF) else Color(
                        0xFFF7F7F7
                    )
                ),
            contentAlignment = Alignment.TopStart
        ) {
            Column {
                Text(
                    text = goods.goods_name,
                    fontSize = 14.sp,
                    color = Color.Black,
                    textAlign = TextAlign.Center,
                    modifier = Modifier
                        .padding(top = 10.dp)
                        .fillMaxWidth()
                )
                Text(
                    text = "¥${goods.goods_price}",
                    fontSize = 24.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color.Black,
                    textAlign = TextAlign.Center,
                    modifier = Modifier
                        .padding(top = 10.dp)
                        .fillMaxWidth()
                )
                Text(
                    text = "¥${goods.original_price}",
                    fontSize = 12.sp,
                    color = if (isSelected) Color.White else Color(
                        0xFFC7C7C7
                    ),
                    textAlign = TextAlign.Center,
                    modifier = Modifier
                        .padding(top = 10.dp)
                        .fillMaxWidth()
                )
            }
            val originalPrice =
                BigDecimal.valueOf(goods.original_price.toDouble())
            val goodsPrice = BigDecimal.valueOf(goods.goods_price.toDouble())
            val discount = originalPrice.subtract(goodsPrice)
                .setScale(2, RoundingMode.HALF_UP)
            Text(
                text = "已减¥${discount}",
                fontSize = 12.sp,
                color = Color.Black,
                modifier = Modifier
                    .align(Alignment.BottomCenter)
                    .fillMaxWidth()
                    .background(
                        color = if (isSelected) Color(0xFF88B6FF) else Color(
                            0xFFF1F1F1
                        )
                    )
                    .padding(4.dp),
                textAlign = TextAlign.Center
            )
        }
    }
}